import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { LocalStorage } from '@core/local.storage';
import { _HttpClient } from '@delon/theme';
import { DatePipe } from '@angular/common';
import { TreeTools } from '@core/treeTools.tool';

@Component({
    selector: 'app-operamodal',
    templateUrl: './operaModal.component.html',
    styleUrls: ['./workAreaWeave.component.less']
})
export class OperaModalComponent implements OnInit {
    @Input() apiUrl: any;
    @Input() operaType: any;
    @Input() width: any;
    @Input() responseData: any;
    @Input() popTitle: any;
    @Output() public closeModal = new EventEmitter();
    @Output() public reloadData = new EventEmitter();

    Visible = false;
    operaData: any = {};
    objData: any = {};
    thisData: any = { 'count': 0, 'vcId': [] };
    isLoading: any = {};

    weekSelect: any = [
        { id: 1, name: '第1周' }, { id: 2, name: '第2周' }, { id: 3, name: '第3周' }, { id: 4, name: '第4周' }, { id: 5, name: '第5周' }, { id: 6, name: '第6周' },
        { id: 7, name: '第1周--增补' }, { id: 8, name: '第2周--增补' }, { id: 9, name: '第3周--增补' }, { id: 10, name: '第4周--增补' }, { id: 11, name: '第5周--增补' }, { id: 12, name: '第6周--增补' }
    ];
    monthSelect: any = [
        { id: 1, name: '1月' }, { id: 2, name: '2月' }, { id: 3, name: '3月' }, { id: 4, name: '4月' },
        { id: 5, name: '5月' }, { id: 6, name: '6月' }, { id: 7, name: '7月' }, { id: 8, name: '8月' },
        { id: 9, name: '9月' }, { id: 10, name: '10月' }, { id: 11, name: '11月' }, { id: 12, name: '12月' }
    ];
    treeNodeStyle = { 'max-height': '300px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };

    constructor(
        private msg: NzMessageService,
        public ls: LocalStorage,
        private datePipe: DatePipe,
        public treeTools: TreeTools,
        private http: _HttpClient
    ) { }

    ngOnInit() {
        this.operaData.vcYear = this.datePipe.transform(new Date(), 'yyyy');
        this.operaData.vcMonth = Number(this.datePipe.transform(new Date(), 'M'));
        this.operaData.vcWeeknum = Math.ceil(new Date().getDate() / 7) + Math.ceil(new Date().getDay() / 7);
        this.operaData.dtWeekedate = this.datePipe.transform(new Date(), 'yyyy-MM-dd');
        this.operaData.dtWeeksdate = this.datePipe.transform(new Date(this.operaData.dtWeekedate).getTime() - 7 * 24 * 60 * 60 * 1000, 'yyyy-MM-dd');
        this.operaData.proposal = '同意';

        if (!this.width) {
            this.width = '500';
        }
        switch (this.operaType) {
            case 'add':
                this.getApi(() => {
                    // to do
                });
                break;
            case 'sign':
            case 'tb':
            case 'approve':
            case 'del':
                let delStatus = false;
                for (const item of this.responseData) {
                    if (item.checked) {
                        this.objData = item;
                        this.thisData.count += 1;
                        this.thisData.vcId.push(item.vcId);
                        delStatus = true;
                    }
                }
                if (delStatus) {
                    this.getApi(() => {
                        // to do
                    });
                } else {
                    this.Cancel();
                    this.msg.error('请选择数据进行操作');
                }
                break;
        }
    }
    getApi(callback) {
        setTimeout(() => {
            this.Visible = true;
            if (callback) {
                callback()
            }
        }, 200);
    }
    opera(type) {
        if (!this.operaData.proposal && type !== 'qx') {
            this.msg.error('请填写审批意见');
            return;
        }
        const typeName = window.location.href.split('/')[window.location.href.split('/').length - 1];
        const name = JSON.parse(this.ls.get('loginInfo')).VC_NAME;
        const time = this.datePipe.transform(new Date(), 'yyyy-MM-dd HH:mm:ss');
        const param: any = {};
        param.vcId = JSON.stringify(this.objData.vcId);

        switch (type) {
            case 'qx':
                this.Visible = false;
                setTimeout(() => {
                    this.closeModal.emit();
                }, 200);
                return;
            case 'bh':
                this.isLoading.Bh = true;
                setTimeout(() => {
                    this.isLoading.Bh = false;
                }, 2000);
                switch (typeName) {
                    case 'station':
                        param.vcStatus = '3';
                        param.vcWsProposal = this.operaData.proposal;
                        param.vcWsProposalPerson = name;
                        param.dtWsProposalTime = time;
                        break;
                    case 'seg':
                        param.vcStatus = '5';
                        param.vcSegProposal = this.operaData.proposal;
                        param.vcSegProposalPerson = name;
                        param.dtSegProposalTime = time;
                        break;
                }
                break;
            case 'pz':
                this.isLoading.Pz = true;
                setTimeout(() => {
                    this.isLoading.Pz = false;
                }, 2000);
                switch (typeName) {
                    case 'station':
                        param.vcStatus = '2';
                        param.vcWsProposal = this.operaData.proposal;
                        param.vcWsProposalPerson = name;
                        param.dtWsProposalTime = time;
                        break;
                    case 'seg':
                        param.vcStatus = '4';
                        param.vcSegProposal = this.operaData.proposal;
                        param.vcSegProposalPerson = name;
                        param.dtSegProposalTime = time;
                        break;
                }
                break;
        }
        this.http.post(this.apiUrl, param).subscribe((res: any) => {
            if (!res.code) {
                this.Visible = false;
                setTimeout(() => {
                    this.closeModal.emit();
                    this.reloadData.emit();
                }, 200);
            } else {
                if (res.msg) {
                    this.msg.error(res.msg);
                } else {
                    this.msg.error('操作失败');
                }
            }
        });
    }
    Ok() {
        this.isLoading.Ok = true;
        // setTimeout(() => {
        //     this.isLoading.Ok = false;
        // }, 2000);
        let param: any = {};
        switch (this.operaType) {
            case 'add':
                if (!this.operaData.vcWeeknum || !this.operaData.vcMonth || !this.operaData.vcYear ||
                    !this.operaData.dtWeeksdate || !this.operaData.dtWeekedate) {
                    this.msg.error('必填项不能为空', { nzDuration: 1000 });
                    return;
                }
                const loginInfo = JSON.parse(this.ls.get('loginInfo'));
                param.vcWeeknum = JSON.stringify(this.operaData.vcWeeknum);
                param.vcMonth = JSON.stringify(this.operaData.vcMonth);
                param.vcYear = this.operaData.vcYear instanceof Date ? this.datePipe.transform(this.operaData.vcYear, 'yyyy') : this.operaData.vcYear;
                param.dtWeeksdate = this.datePipe.transform(this.operaData.dtWeeksdate, 'yyyy-MM-dd');
                param.dtWeekedate = this.datePipe.transform(this.operaData.dtWeekedate, 'yyyy-MM-dd');
                param.vcWsOrgId = loginInfo.VC_ORG$ID;
                param.vcStatus = '1';
                param.vcUserid = loginInfo.VC_NUMBER;
                param.dtEditdate = this.datePipe.transform(new Date(), 'yyyy-MM-dd');
                break;
            case 'sign':
                param.vcStatus = '0';
                param.proposal = this.operaData.proposal;
                param.vcWaCheck = JSON.parse(this.ls.get('loginInfo')).VC_NAME;
                break;
            case 'tb':
                param.vcStatus = '2';
                param.vcId = this.thisData.vcId.toString();
                break;
            case 'del':
                param = this.thisData.vcId;
                break;
        }
        this.http.post(this.apiUrl, param).subscribe((res: any) => {
            this.msg.info(res.msg);
            if (!res.code) {
                this.Visible = false;
                setTimeout(() => {
                    this.closeModal.emit();
                    this.reloadData.emit();
                }, 200);
            }
            this.isLoading.Ok = false;
        });
    }
    Cancel() {
        this.Visible = false;
        setTimeout(() => {
            this.closeModal.emit();
        }, 200);
    }
}
